package com.founder.gateway.component.transport.sentinel;

import javax.annotation.PreDestroy;

import org.setu.framework.thread.SetuThread;
import org.setu.framework.util.SetuApplicationUtil;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
/**
 * @author hfl
 * 2018年12月4日
 */
@Component
public class SentinelServerListener implements  ApplicationListener<ContextRefreshedEvent> {
   
    
    /**
     * netty服务启动线程 
     * @author hfl
     * 2018年12月4日
     */
	private class NettyServerThread extends SetuThread {
		@Override
		public void runCustom() {
			try {
				SetuApplicationUtil.getBean(SentinelServer.class).start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
    }

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		if(event.getApplicationContext().getParent()==null) {
			try {
				Thread thread=new Thread(new NettyServerThread());
				thread.setName("Sentinel Server");
				thread.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	@PreDestroy
    public void close() throws Exception {
		SetuApplicationUtil.getBean(SentinelServer.class).stop();
    }

}